package com.example.qincetest.second

import android.content.Intent
import android.content.res.Configuration
import android.graphics.Rect
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatDelegate
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.qincetest.BaseActivity
import com.example.qincetest.R
import timber.log.Timber

class SecondActivity : BaseActivity() {
    var isDarkMode = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        val recyclerview = findViewById<RecyclerView>(R.id.list)
        recyclerview.layoutManager = LinearLayoutManager(this)
        val adapter = ItemRecyclerViewAdapter()
        recyclerview.adapter = adapter
        val list = mutableListOf<PlaceholderItem>()
        for (i in 1..100){
            list.add(PlaceholderItem(i.toLong(),"bbbb$i","aaaaaaaaaaaa$i"))
        }
        adapter.dataList.addAll(list)
        adapter.notifyDataSetChanged()

        // 添加按钮点击事件
        findViewById<Button>(R.id.btnOpen).setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        }

        findViewById<Button>(R.id.btnDark).setOnClickListener{

            if (isDarkMode) {
                isDarkMode = true
                changeDarkTheme()
            } else {
                isDarkMode = false
                changeDarkTheme()
            }
        }

        recyclerview.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                checkExposure(recyclerView)
            }
        })
    }

    // 记录已曝光的item
    private val exposuredItems = HashSet<String>()
    private fun checkExposure(recyclerView: RecyclerView) {
        val layoutManager = recyclerView.layoutManager as? LinearLayoutManager ?: return

        // 获取可见范围内的第一个和最后一个item位置
        val firstVisible = layoutManager.findFirstVisibleItemPosition()
        val lastVisible = layoutManager.findLastVisibleItemPosition()

        // 遍历可见item
        for (i in firstVisible..lastVisible) {
            val itemView = layoutManager.findViewByPosition(i) ?: continue
            val item = recyclerView.adapter?.getItemId(i).toString()

            // 检查是否已曝光
            if (!exposuredItems.contains(item) && isViewVisible(itemView)) {
                // 记录曝光
                exposuredItems.add(item)
                // 上报曝光事件
                Timber.e("exposuredItems ${item}")
            }
        }
    }

    private fun isViewVisible(view: View): Boolean {
        // 检查view是否在屏幕内可见
        val rect = Rect()
        return view.getGlobalVisibleRect(rect) &&
                rect.height() >= view.height * 0.5 // 曝光比例阈值
    }

    //设置切换模式
    private fun changeDarkTheme() {
        isDarkMode = !isDarkMode
        if (isDarkMode) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        recreate()
    }
}
